// pages/issue/issue.js
const app = getApp()
import { _sendComment} from '../../utils/qiaofinn.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgList:[],   //图片列表
    img_ids:[],   //上传图片返回的id列表
    loadCount:0,  //上传完成个数
    content:'',  //日志内容
    model_name:'',
    object_id:0,
    cursor:0,    //游标
    showEmoji:false,
    emojiSource: 'https://res.wx.qq.com/op_res/eROMsLpnNC10dC40vzF8qviz63ic7ATlbGg20lr5pYykOwHRbLZFUhgg23RtVorX',
  },
  // 发布按钮
  publishTap:function(){
    const {model_name,content,object_id,img_ids} = this.data
    const parseContent=this.parseEmoji(content)  
    let data = {
      content: content,
      model_name:model_name,
      object_id:object_id,
      img_ids:img_ids,
      parseContent:JSON.stringify(parseContent)
    }
    _sendComment(data,res=>{
      if(res.statusCode==200 && res.data.status == true){
        wx.navigateBack({
          delta: 1
        })
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  // content输入框失去焦点时触发
  commentInput:function(res){
    this.data.cursor = res.detail.cursor || 0
    this.setData({
      content:res.detail.value
    })
  },
  // 选择图片
  ChooseImage() {
    wx.chooseImage({
      count: 6, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        const tempFilePaths = res.tempFilePaths
        this.setData({
          loadModal:true,
          toatalCount:tempFilePaths.length
        })
        for(var i in tempFilePaths){
          this.uploadFileToServer(this,tempFilePaths[i],i)
        }
        this.setData({
          loadModal:false
        })
      }
    });
  },
  uploadFileToServer(that,tempFilePath,i){
 //  图片长传
    wx.uploadFile({
      url: app.globalData.qiaofinn + '/system/upload_file/', 
      filePath: tempFilePath,
      name: 'file',
      formData: {
        'type': 'comment'
      },
      success (res){
        const data = JSON.parse(res.data)
        that.data.imgList.push(tempFilePath)
        that.data.img_ids.push(data['data'])
        
        that.setData({
          imgList:that.data.imgList,
          img_ids:that.data.img_ids,
          loadCount:i
        })
      },
      
    })
  },
  // 查看图片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  // 删除图片
  DelImg(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
    this.data.img_ids.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: this.data.imgList,
      img_ids:this.data.img_ids
    })
  },
  imageTap:function(){
    this.setData({
      showEmoji:!this.data.showEmoji
    })
  },
  // 插入表情
  insertEmoji(evt) {
    const emotionName = evt.detail.emotionName

    const {cursor,content} = this.data
    const newContent =
    content.slice(0, cursor) + emotionName + content.slice(cursor)
    this.setData({
      content: newContent,
      cursor: cursor + emotionName.length
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      model_name:options.model_name,
      object_id:options.object_id
    })
     //用户对评论的解析 
     const emojiInstance = this.selectComponent('.mp-emoji') 
     this.parseEmoji = emojiInstance.parseEmoji;  
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})